package com.cmxh.mycloudnotes.controller;


import com.alibaba.excel.EasyExcel;
import com.cmxh.mycloudnotes.Service.ArticleService;
import com.cmxh.mycloudnotes.pojo.Article;
import com.cmxh.mycloudnotes.pojo.Article01;
import com.cmxh.mycloudnotes.util.Code01;
import com.cmxh.mycloudnotes.util.PageUtil;
import com.cmxh.mycloudnotes.util.Result01;
import com.cmxh.mycloudnotes.util.UserListener;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    private BitSet outputStream;

    @RequestMapping("/findAll")
    public Result01 findAll(@RequestBody PageUtil pageUtil) {
        PageInfo<Article> xx = articleService.findAll(pageUtil);
        if (xx != null){
            return new Result01( Code01.GET_OK, xx, "查询成功" );
        }else{
            return new Result01( Code01.GET_ERR, "查询失败" );
        }


    }
    @RequestMapping("/pdateZt1/{id}")
    public Result01 pdateZt1(@PathVariable Integer id) {
        int xx = articleService.pdateZt1(id);
        if (xx > 0){
            return new Result01( Code01.DELETE_OK, "删除成功" );
        }else{
            return new Result01( Code01.DELETE_ERR, "删除失败" );
        }
    }
    @PostMapping(value = "/importData")
    public Result01 importData(MultipartFile file){

        try {
            // 获取前端上传文件的字节流  读流
            InputStream inputStream = file.getInputStream();
            // 从这个文件流中读取数据并且把这一行数据封装成文章对象  并且把这7个文章对象存放到一个集合中
            /**
             * 第一个参数 客户端的读流
             * 第二个参数 我们需要把excel中每一行封装成什么对象  你就写Article类
             *
             * sheet(0)  读取第一个工作表的数据
             * .doReadSync()  异步读取
             */
            List<Article01> list= EasyExcel.read(inputStream)
                    .registerReadListener( new UserListener()).
                    head(Article.class)
                    .sheet("学生").doReadSync();


            return new Result01(Code01.UPDATE_OK,"导入成功");

        } catch (IOException e) {
            e.printStackTrace();
            return new Result01(Code01.UPDATE_ERR,"导入失败");
        }
    }

    @RequestMapping("/exportdata/{id}")
    public ResponseEntity<byte[]> exportdata(@PathVariable Integer id) {

        List<Article> xx = articleService.findAll(id);


        //Byte:字节 Array:数组 0utputstream:写流
        ByteArrayOutputStream bos =new ByteArrayOutputStream();

        //第几个工作表
        EasyExcel.write( bos,Article.class).sheet("文章信息表").doWrite(xx);

        // 返回响应实体


        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=people.xlsx");

        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    } 
    
}
